home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 May: Tool Chest / Developer CD Series May 1996 (Tool Chest) (Apple Computer) (1996).iso / Tool Chest / Developer Utilities / Installer 4.0.3 SDK / Script Examples / Search Proc [insp] Example / FindTargetFile.c next >
Encoding:
Text File  |  1994-11-15  |  12.6 KB  |  332 lines  |  [TEXT/MPS ]

  1. //
  2. //    FindTargetFile.c
  3. //
  4. //        A file search proc for use with the Apple Installer 4.x.
  5. //
  6. //        6/1/94 - original code by Rob "Lunatic" Moore
  7. //        6/8/94 • mark young - ≈.2 through ≈.4 backup copies 
  8. //                    - added code to return different values to installer depending
  9. //                    on whether files were found or not, or if count of found files is invalid
  10. //                    - added stub code to simulate nil found file list handle, and returning
  11. //                    the various possible result codes ( see bug #1148502 )
  12. //                    NOTE:     I noticed that the return value must be kSearchSuccessful for an installer
  13. //                            rule clause ( referencing an 'insp' ) to fire as true.
  14. //                    NOTE:    I noticed that the two non-zero return values ( kFatalSearchError and
  15. //                            kCancelSearchAndInstallation ) have no effect on cancelling installation
  16. //                            when called from within an installer rule clause ( referencing an 'insp' )
  17. //
  18. //
  19. //        Copyright 1993-1994, Apple Computer, Inc., All Rights Reserved
  20. //
  21.  
  22. #include <Files.h>
  23. #include <GestaltEqu.h>
  24. #include <Memory.h>
  25. #include <OSUtils.h>
  26. #include <Packages.h>
  27. #include <TextUtils.h>    // newest MPW puts NumToString in this file !!
  28.  
  29. #ifndef __CallbackDispatcherHeader__
  30. #include "CallbackDispatcherHeader.h"
  31. #endif
  32.  
  33. #ifndef __ActionHandlerHeader__
  34. #include "ActionHandlerHeader.h"
  35. #endif
  36.  
  37. #ifndef __InstallerMemoryFuncsHeader__
  38. #include "InstallerMemoryFuncsHeader.h"
  39. #endif
  40.  
  41. #ifndef __SearchProcedureHeader__
  42. #include "SearchProcedureHeader.h"
  43. #endif
  44.  
  45.  
  46. void DisplaySearchInfo( SearchProcedurePBPtr pSearchProcedurePBPtr, SearchResult pResultCode );
  47. FoundFileArrayHdl MakeFoundFilesArrayHdl();
  48. OSErr    AddFSSpecToFoundFilesArrayHdl(    FoundFileArrayHdl    pFoundFileArrayHdl,
  49.                                         FSSpec                pFSSpec );
  50. void PrintLine( ProcPtr pCallBackProcPtr, Str255 pParam0, Str255 pParam1, Str255 pParam2, Str255 pParam3 );
  51.  
  52. SearchResult FindTargetFile( SearchProcedurePBPtr pSearchProcedurePBPtr )
  53. {
  54.     #define            kMaxNumOfMatches    40
  55.     #define            kOptBufferSize        0x100
  56.  
  57.     SearchResult        result;
  58.     FoundFileArrayHdl    myFoundFileArrayHdl;
  59.  
  60.     long                theSysVersionNum;
  61.  
  62.     OSErr                theErr;
  63.     CInfoPBRec            theFirstSearchCriteria;
  64.     CInfoPBRec            theSecondSearchCriteria;
  65.     CSParam                theCatParamBlock;
  66.     FSSpec                theMatchedFSSpecArray[kMaxNumOfMatches];
  67.     char                theOptionalBuffer[kOptBufferSize];
  68.     short                theByteCtr;
  69.     short                theFoundFSSpecCtr;
  70.     FSSpec                theTempFSSpec;
  71.  
  72. // dummy code to simulate all possibilities of passing back a nil handle to search file list
  73. //myFoundFileArrayHdl = NULL;
  74. //return( kSearchSuccessful );
  75. //return( kFatalSearchError );
  76. //return( kCancelSearchAndInstallation );
  77.  
  78.     myFoundFileArrayHdl = MakeFoundFilesArrayHdl( pSearchProcedurePBPtr->fCallBackProcPtr );
  79.     
  80.     if( myFoundFileArrayHdl != NULL ) {
  81.     
  82.         Gestalt( 'sysv', &theSysVersionNum );
  83.         if( theSysVersionNum >= 0x00000700 ) {
  84.         
  85.             theCatParamBlock.ioCompletion        = NULL;
  86.             theCatParamBlock.ioNamePtr            = NULL;
  87.             theCatParamBlock.ioVRefNum            = pSearchProcedurePBPtr->fTargetVRefNum;
  88.             
  89.             theCatParamBlock.ioMatchPtr            = (FSSpecArrayPtr)theMatchedFSSpecArray;    /* match array */
  90.             theCatParamBlock.ioReqMatchCount    = kMaxNumOfMatches;                            /* maximum allowable matches */
  91.             theCatParamBlock.ioSearchBits        = fsSBFlFndrInfo + fsSBFlAttrib;            /* search criteria selector */
  92.             theCatParamBlock.ioSearchInfo1        = &theFirstSearchCriteria;                    /* search values and range lower bounds */
  93.             theCatParamBlock.ioSearchInfo2        = &theSecondSearchCriteria;                    /* search values and range upper bounds */
  94.             theCatParamBlock.ioSearchTime        = 0;                                        /* length of time to run search */
  95.             theCatParamBlock.ioCatPosition.initialize    = 0;                                /* current position in the catalog */
  96.             theCatParamBlock.ioOptBuffer        = theOptionalBuffer;                        /* optional performance enhancement buffer */
  97.             theCatParamBlock.ioOptBufSize        = kOptBufferSize;                            /* size of buffer pointed to by ioOptBuffer */
  98.         
  99.             theFirstSearchCriteria.hFileInfo.ioFlFndrInfo.fdType        = pSearchProcedurePBPtr->fFileSpecType;            /* An application */
  100.             theFirstSearchCriteria.hFileInfo.ioFlFndrInfo.fdCreator        = pSearchProcedurePBPtr->fFileSpecCreator;            /* TeachText creator */
  101.             theFirstSearchCriteria.hFileInfo.ioFlAttrib                    = 0x00;                /* A File */
  102.     
  103.             theSecondSearchCriteria.hFileInfo.ioFlFndrInfo.fdType        = 0xFFFFFFFF;        /*the type of the file*/
  104.             theSecondSearchCriteria.hFileInfo.ioFlFndrInfo.fdCreator    = 0xFFFFFFFF;        /*file's creator*/
  105.             theSecondSearchCriteria.hFileInfo.ioFlFndrInfo.fdFlags        = 0x0000;            /*flags ex. hasbundle,invisible,locked, etc.*/
  106.             theSecondSearchCriteria.hFileInfo.ioFlFndrInfo.fdLocation.h    = 0x0000;            /*file's location in folder*/
  107.             theSecondSearchCriteria.hFileInfo.ioFlFndrInfo.fdLocation.v    = 0x0000;            /*file's location in folder*/
  108.             theSecondSearchCriteria.hFileInfo.ioFlFndrInfo.fdFldr        = 0x0000;            /*folder containing file*/
  109.             theSecondSearchCriteria.hFileInfo.ioFlAttrib                = 0x10;                /* Check file/directory bit */
  110.             
  111.             theErr = PBCatSearchSync( &theCatParamBlock );
  112.         
  113.             // Add each found
  114.             for( theFoundFSSpecCtr = 0; theFoundFSSpecCtr < theCatParamBlock.ioActMatchCount; theFoundFSSpecCtr++ ) {
  115.                 theTempFSSpec.vRefNum    = theMatchedFSSpecArray[theFoundFSSpecCtr].vRefNum;
  116.                 theTempFSSpec.parID    = theMatchedFSSpecArray[theFoundFSSpecCtr].parID;
  117.                 
  118.                 for( theByteCtr = 0; theByteCtr <= theMatchedFSSpecArray[theFoundFSSpecCtr].name[0]; theByteCtr++ )
  119.                     theTempFSSpec.name[theByteCtr]    = theMatchedFSSpecArray[theFoundFSSpecCtr].name[theByteCtr];
  120.  
  121.                 AddFSSpecToFoundFilesArrayHdl( myFoundFileArrayHdl,theTempFSSpec );    
  122.             }
  123.         
  124.         }
  125.         else {
  126.         
  127.             // Implement the pre-7.0 search code here
  128.         
  129.         }
  130.             
  131.         
  132.         pSearchProcedurePBPtr->fFoundFilesArray = myFoundFileArrayHdl;
  133.         
  134.         // one or more files were found
  135.         // this causes installation to continue normally, no dlogs are displayed
  136.         if ( theCatParamBlock.ioActMatchCount > 0 )
  137.             result = kSearchSuccessful;
  138.             
  139.         // no files were found
  140.         // this causes dlog "Installer doc is damaged..." to be displayed, cancels installation
  141.         else if ( theCatParamBlock.ioActMatchCount == 0 )
  142.             result = kFatalSearchError;
  143.             
  144.         // illegal file count
  145.         // this causes dlog "Installation was cancelled..." to be displayed, cancels installation
  146.         else result = kCancelSearchAndInstallation;
  147.                         
  148.         DisplaySearchInfo( pSearchProcedurePBPtr, result );
  149.  
  150.     }
  151.     else 
  152.         // this causes dlog "Installer doc is damaged..." to be displayed, cancels installation
  153.         result = kFatalSearchError;
  154.             
  155.     return result;
  156. }
  157.  
  158.  
  159.  
  160. // ***********************************************************************************************
  161. // ******************************** FoundFileArrayHdl routines ***********************************
  162. // ***********************************************************************************************
  163.  
  164. FoundFileArrayHdl MakeFoundFilesArrayHdl( ProcPtr    pCallBackProcPtr)
  165. {
  166.     return (FoundFileArrayHdl)INewHandle( pCallBackProcPtr, 0 );
  167. }
  168.  
  169. OSErr    AddFSSpecToFoundFilesArrayHdl(    FoundFileArrayHdl    pFoundFileArrayHdl,
  170.                                         FSSpec                pFSSpec )
  171. {
  172.     FSSpecPtr            newFSSpecPtr;
  173.     OSErr                theErr     = noErr;
  174.     Size                orgSize = GetHandleSize( (Handle)pFoundFileArrayHdl );
  175.     
  176.     if( pFoundFileArrayHdl != NULL ) {
  177.         SetHandleSize( (Handle)pFoundFileArrayHdl, orgSize + sizeof( FoundFileRec ) );
  178.         theErr = MemError();
  179.         
  180.         if( theErr == noErr ) {
  181.             newFSSpecPtr = (FSSpecPtr)(StripAddress(*pFoundFileArrayHdl) + orgSize);
  182.             *newFSSpecPtr = pFSSpec;
  183.         }
  184.     }
  185.     
  186.     return theErr;
  187. }
  188.  
  189.  
  190.  
  191. // -------------------------------------------------------------------------------------------------------------------------------------
  192. // The routines below are for displaying the contents of the parameter block passed to the Setup Function.
  193. // Call DisplayPreferenceInfo to write this information to the Installer Debugger.
  194.  
  195. void MakeFilePath( FSSpec* pFileFSSpec, Str255  pFilePath )
  196. {
  197.     
  198.     CInfoPBRec        cPBRec;
  199.     Str255            directoryName = "";
  200.     short            i;
  201.     OSErr            theErr;
  202.  
  203.     // Get the file name
  204.     for( i=0;i<=pFileFSSpec->name[0];i++)
  205.         pFilePath[i] = pFileFSSpec->name[i];
  206.  
  207.     cPBRec.hFileInfo.ioCompletion = NULL;
  208.     cPBRec.hFileInfo.ioNamePtr = directoryName;
  209.     cPBRec.hFileInfo.ioFDirIndex = -1;
  210.     cPBRec.hFileInfo.ioDirID = pFileFSSpec->parID;
  211.     cPBRec.hFileInfo.ioVRefNum = pFileFSSpec->vRefNum;
  212.     theErr = PBGetCatInfo( &cPBRec, 0 );
  213.  
  214.     // Preappend directoryName
  215.     BlockMove( pFilePath + 1, pFilePath + directoryName[0] + 2, pFilePath[0] );
  216.     for( i=1;i<=directoryName[0];i++)
  217.         pFilePath[i] = directoryName[i];
  218.     pFilePath[i] = ':';
  219.     pFilePath[0] += directoryName[0] + 1;
  220.  
  221.     while ( theErr == noErr && cPBRec.dirInfo.ioDrParID != 1 && pFilePath[0] + directoryName[0] + 1 < 255 ) {
  222.  
  223.     // Preappend directoryName
  224.         cPBRec.hFileInfo.ioDirID = cPBRec.dirInfo.ioDrParID;
  225.         cPBRec.hFileInfo.ioFDirIndex = -1;
  226.         theErr = PBGetCatInfo( &cPBRec, 0 );
  227.  
  228.         if( theErr == noErr ) {
  229.             // Preappend directoryName
  230.             BlockMove( pFilePath + 1, pFilePath + directoryName[0] + 2, pFilePath[0] );
  231.             for( i=1;i<=directoryName[0];i++)
  232.                 pFilePath[i] = directoryName[i];
  233.             pFilePath[i] = ':';
  234.             pFilePath[0] += directoryName[0] + 1;
  235.         }
  236.     }
  237.  
  238. }
  239.  
  240. void PrintLine( ProcPtr pCallBackProcPtr, Str255 pParam0, Str255 pParam1, Str255 pParam2, Str255 pParam3 )
  241. {
  242.     long    theResult;
  243.     RegisterScriptAction( pCallBackProcPtr, kDebuggingAction, kGenericDebugActID, pParam0, pParam1, pParam2, pParam3, &theResult );    
  244. }
  245.  
  246. void DisplaySearchInfo( SearchProcedurePBPtr pSearchProcedurePBPtr, SearchResult pResultCode )
  247. {
  248.  
  249. StringPtr                    kBeginCallPart1                    = "\p=>========================== Begin File Search Function Call ====";
  250. StringPtr                    kBeginCallPart3                    = "\p==================================================";
  251.  
  252. StringPtr                    kEndCallPart1                    = "\p-<-------------------------- End File Search Function Call ------ ";
  253. StringPtr                    kEndCallPart3                    = "\p ----------------------------\n";
  254.  
  255. StringPtr                    kResultText                        = "\p       Result Code: ";
  256. StringPtr                    kRefConText                        = "\p            RefCon: ";
  257. StringPtr                    kFileTypeText                    = "\p              Type: ";
  258. StringPtr                    kFileCreatorText                = "\p           Creator: ";
  259. StringPtr                    kFileFileSpecPathText            = "\p    File Spec Path: ";
  260.  
  261. StringPtr                    kFilesFoundTitleText            = "\pFound Files...";
  262. StringPtr                    kNoFilesFoundTitleText            = "\pNo Files Found!";
  263. StringPtr                    kFoundFilePathText                = "\p        Found File:  ";
  264.  
  265. StringPtr                    kTargetFolderPathText            = "\p    Target Folder Path:  ";
  266. StringPtr                    kSystemDiskText                    = "\p      System Disk Name:  ";
  267.  
  268. Str255                        tempNumStr;    
  269. Str255                        tempStr255;    
  270.  
  271. short                        numOfFoundFiles;
  272. short                        foundFileCtr;
  273. FoundFileRec                theFoundFileRec;
  274.  
  275.     // -- Beginning line
  276.     PrintLine( pSearchProcedurePBPtr->fCallBackProcPtr, kBeginCallPart1, kBeginCallPart3, "\p", "\p" );
  277.  
  278.     // —— Print File Path from File Spec
  279.     PrintLine( pSearchProcedurePBPtr->fCallBackProcPtr, kFileFileSpecPathText, pSearchProcedurePBPtr->fFileSpecPath, "\p", "\p" );
  280.     
  281.     // —— Print Type from File Spec
  282.     BlockMove( &(pSearchProcedurePBPtr->fFileSpecType), &(tempNumStr[2]), 4 );
  283.     tempNumStr[1] = '\'';
  284.     tempNumStr[6] = '\'';
  285.     tempNumStr[0] = 6;
  286.     PrintLine( pSearchProcedurePBPtr->fCallBackProcPtr, kFileTypeText, tempNumStr, "\p", "\p" );
  287.     
  288.     // —— Print Creator from File Spec
  289.     BlockMove( &(pSearchProcedurePBPtr->fFileSpecCreator), &(tempNumStr[2]), 4 );
  290.     tempNumStr[1] = '\'';
  291.     tempNumStr[6] = '\'';
  292.     tempNumStr[0] = 6;
  293.     PrintLine( pSearchProcedurePBPtr->fCallBackProcPtr, kFileCreatorText, tempNumStr, "\p", "\p" );
  294.     
  295.     // —— Print RefCon
  296.     NumToString( pSearchProcedurePBPtr->fRefCon, tempNumStr );
  297.     PrintLine( pSearchProcedurePBPtr->fCallBackProcPtr, kRefConText, tempNumStr, "\p", "\p" );
  298.     
  299.     if( pSearchProcedurePBPtr->fFoundFilesArray != NULL ) {
  300.         numOfFoundFiles = GetHandleSize( (Handle)pSearchProcedurePBPtr->fFoundFilesArray ) / sizeof( FoundFileRec);
  301.         
  302.         if( numOfFoundFiles > 0 )
  303.             PrintLine( pSearchProcedurePBPtr->fCallBackProcPtr, kFilesFoundTitleText, "\p", "\p", "\p" );
  304.         else
  305.             PrintLine( pSearchProcedurePBPtr->fCallBackProcPtr, kNoFilesFoundTitleText, "\p", "\p", "\p" );
  306.         
  307.         // Print found file paths
  308.         for( foundFileCtr = 0; foundFileCtr < numOfFoundFiles; foundFileCtr++ ) {
  309.             theFoundFileRec = (*(pSearchProcedurePBPtr->fFoundFilesArray))[foundFileCtr];
  310.             MakeFilePath( (FSSpec*)&theFoundFileRec, tempStr255 );
  311.             PrintLine( pSearchProcedurePBPtr->fCallBackProcPtr, kFoundFilePathText, tempStr255, "\p", "\p" );
  312.         }
  313.     }
  314.  
  315.     // -- Ending line
  316.     NumToString( pResultCode, tempNumStr );
  317.     PrintLine( pSearchProcedurePBPtr->fCallBackProcPtr, kEndCallPart1, kResultText, tempNumStr, kEndCallPart3 );
  318. }
  319.  
  320.  
  321. // Because this include contains functions that
  322. // will be compiled, and we want to make sure
  323. // that our main entry point 'GetRsrcVersion()'
  324. // is placed at the top of the code resource
  325. // that is generated, this include statement
  326. // is put at the end of this file. Even better
  327. // method would be to compile this included file
  328. // as a seperate library and to link that library
  329. // to the GetRsrcVersion() object file.
  330. #include "ActionHandlerCBGlue.c"
  331. #include "InstallerMemoryCBGlue.c"
  332.